#include "server.h"
//获取随机盐值
int getRandomSalt(char* salt){
    char random[65] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
    strcat(salt,"$6$");
    srand(time(NULL));
    for (int i = 3; i < 19; ++i){
        salt[i] = random[rand()%64];
    }
    strcat(salt,"$");
    return 0;
}
//登录
int login_send_salt(int netfd,char* usrname,MYSQL* mysql){
    train_t train;
    
    char sql[1024] = {0};
    sprintf(sql,"select * from user where name = '%s';",usrname);
    int qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
    }
    LOG_ERROR_CHECK(qret,!0,"mysql_query in login.");

    char salt[30] = {0};
    MYSQL_RES* result = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row == NULL){
        printf("该用户第一次登录。\n");
        getRandomSalt(salt);
        sprintf(sql,"insert into user (name,salt) value ('%s','%s');",usrname,salt);
        qret = mysql_query(mysql,sql);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
        }
    }else{
        memcpy(salt,row[2],strlen(row[2]) + 1);
    }

    train.tag = LOGIN;
    train.length = strlen(salt) + 1;
    memcpy(train.data,salt,train.length);
    send(netfd,&train,sizeof(train),MSG_NOSIGNAL);
    return 0;
}

int login_check(int netfd,char* usrname,char* encrypted_passworduser,MYSQL* mysql){
    train_t train;
    char sql[1024] = {0};
    sprintf(sql,"select pwd from user where name = '%s';",usrname);
    int qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
    }
    LOG_ERROR_CHECK(qret,!0,"mysql_query in login.");

    char encrypted_password[1024] = {0};
    MYSQL_RES* result = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(result);
    if(row[0] == NULL){
        printf("该用户第一次登录。\n");
        train.tag = FILEEND;
        send(netfd,&train,sizeof(train),MSG_NOSIGNAL);
        sprintf(sql,"update user set pwd = '%s' where name = '%s';",encrypted_passworduser,usrname);
        qret = mysql_query(mysql,sql);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
        }
        sprintf(sql,"insert into file (file_name,user_name,parent_id,type,path) value ('/','%s',-1,'d','/');",usrname);
        qret = mysql_query(mysql,sql);
        if(qret != 0){
            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(mysql));
        }
        syslog(LOG_INFO,"user %s register and log in",usrname);
        return 0;
    }else{
        memcpy(encrypted_password,row[0],strlen(row[0]) + 1);
        if(strcmp(encrypted_password,encrypted_passworduser) == 0){
            train.tag = FILEEND;
            send(netfd,&train,sizeof(train),MSG_NOSIGNAL);
            syslog(LOG_INFO,"user %s log in",usrname);
            return 0;
        }else{
            train.tag = FILEDATA;
            send(netfd,&train,sizeof(train),MSG_NOSIGNAL);
            return -1;
        }
    }
}
